package com.macro.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.service.UmsCouponService;
import com.macro.mall.service.UserMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author：MinxiaoHao
 * @Description:
 * @name：UmsCouponServiceImpl
 * @Date：2024/10/23 16:09
 */
@Service
public class UmsCouponServiceImpl implements UmsCouponService {
    @Autowired
    private SmsCouponMapper couponMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private SmsCouponHistoryMapper couponHistoryMapper;
    @Autowired
    private SmsCouponProductRelationMapper couponProductRelationMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper couponProductCategoryRelationMapper;
    @Autowired
    private UserMemberService userMemberService;
    /**
     * 根据商品id获取优惠券列表
     * @param id 商品id
     */
    @Override
    public List<SmsCoupon> listByProduct(Long id) {
        // 查询指定商品优惠券
        List<SmsCouponProductRelation> productCouponRelations = couponProductRelationMapper.selectByProductId(id);

        List<SmsCoupon> arrayList = productCouponRelations.stream()
                .map(productCoupon -> couponMapper.selectList(productCoupon.getCouponId()))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 查询商品信息
        PmsProduct product = productMapper.selectByPrimaryKey(id);
        if (product != null) {
            // 查询指定分类优惠券
            List<SmsCouponProductCategoryRelation> productCoupons = couponProductCategoryRelationMapper.selectByProductCategoryId(product.getProductCategoryId());
            List<SmsCoupon> categoryCoupons = productCoupons.stream()
                    .map(productCoupon -> couponMapper.selectList(productCoupon.getCouponId()))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());

            arrayList.addAll(categoryCoupons);
        }

        // 查询所有状态为0的优惠券
        List<SmsCoupon> coupons = couponMapper.selectByStatus(0);
        arrayList.addAll(coupons); // 直接添加所有优惠券

        return arrayList;
    }

    /**
     * 商品添加优惠卷
     * @param couponId 优惠卷id
     */
    @Transactional
    @Override
    public int add(Long couponId) {
        //判断商品是否存在
        UmsMember currentMember = userMemberService.getCurrentAdmin();
        //获取优惠券信息，判断数量
        SmsCoupon coupon = couponMapper.selectByPrimaryKey(couponId);
        if (coupon == null) {
            throw new RuntimeException("优惠卷不存在");
        }
        Date now = new Date();
        if (now.before(coupon.getEnableTime())) {
            Asserts.fail("优惠券还没到领取时间");
        }
        //生成领取优惠券历史
        SmsCouponHistory couponHistory = new SmsCouponHistory();
        couponHistory.setCouponId(couponId);
        couponHistory.setCreateTime(now);
        couponHistory.setMemberId(currentMember.getId());
        couponHistory.setMemberNickname(currentMember.getNickname());
        couponHistory.setCouponCode(String.valueOf(now.getTime()));
        //主动领取
        couponHistory.setGetType(1);
        //未使用
        couponHistory.setUseStatus(0);
        int insert = couponHistoryMapper.insert(couponHistory);
        //判断是否数量为1,则删除
        if(coupon.getCount()==1){
            //删除优惠卷
            couponMapper.deleteByPrimaryKey(couponId);
        }else{
            //修改优惠券表的数量、领取数量
            couponMapper.updateById(couponId);
        }
        return insert;
    }
}
